#include "MyPlay.h"
#include <QApplication>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QDebug>
#include <QPixmap>
#include <QIcon>
#include <QWidget>
#include <QMenuBar>
#include <QAction>
#include <QFileDialog>
#include <QFileInfo>

MyPlay::MyPlay(QWidget *parent) : QMainWindow(parent)
{

    playStatus = false;
    QString path = QDir::currentPath();//当前路径
    qDebug() << path;

    resize(640, 480);
    this->setMaximumSize(QSize(800,640));
    this->setMinimumSize(QSize(400,240));

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    this->setLayout(mainLayout);

    QMenuBar *pMenuBar = new QMenuBar;
    pMenuBar = menuBar();

    menu = new QMenu;
    menu = pMenuBar->addMenu("&Load");

    menu->addAction("&Open",this,SLOT(slotOpen()),QKeySequence::Open);
    menu->addSeparator();
    menu->addAction("&Exit",this, SLOT(close()), QKeySequence::Close);

    widget = new QWidget;
    this->setCentralWidget(widget);

    QVBoxLayout *layout = new QVBoxLayout(widget);

    screamShow = new QWidget;

    layout->addWidget(screamShow);

    screamShow->setStyleSheet("background-image:url(./ico/widget.jpg)");

    layout->addWidget(slider = new QSlider(Qt::Horizontal));

    slider->setRange(0,50);
    QHBoxLayout *Hlay = new QHBoxLayout(widget);
    layout->addLayout(Hlay);

    timelab = new QLabel;
    Hlay->addWidget(timelab);
    timelab->setText("00:00:00");

    btnstop = new QPushButton;
    Hlay->addWidget(btnstop);
    btnstop->setIcon(QIcon("./ico/11.ico"));
    btnstop->setIconSize(QSize(50,50));
    btnstop->setFixedSize(40,40);
    connect(btnstop,SIGNAL(clicked()),this,SLOT(pushCloseButton()));

    btnleft = new QPushButton;
    Hlay->addWidget(btnleft);
    btnleft->setIcon(QIcon("./ico/9.ico"));
    btnleft->setIconSize(QSize(50,50));
    btnleft->setFixedSize(40,40);

    btnplay = new QPushButton;
    Hlay->addWidget(btnplay);
    btnplay->setIcon(QIcon("./ico/6.ico"));
    btnplay->setIconSize(QSize(70,70));
    btnplay->setFixedSize(60,60);
    //btnplay->setEnabled(false);
    connect(btnplay,SIGNAL(clicked()),this,SLOT(pushStopButton()));


    btnright = new QPushButton;
    Hlay->addWidget(btnright);
    btnright->setIcon(QIcon("./ico/1.ico"));
    btnright->setIconSize(QSize(50,50));
    btnright->setFixedSize(40,40);

    btnvoice = new QPushButton;
    Hlay->addWidget(btnvoice);
    btnvoice->setIcon(QIcon("./ico/14.ico"));
    btnvoice->setIconSize(QSize(30,30));
    btnvoice->setFixedSize(20,20);

    voiceSlider = new  QSlider(Qt::Horizontal);
    Hlay->addWidget(voiceSlider);

    connect(slider,SIGNAL(sliderMoved(int)),this,SLOT(slotSliderCtl(int)));


}

QTime MyPlay::int_to_time(int second)
{
    int sec = 0, min = 0 ,hour = 0;
    QTime time;
    if(second < 60)
    {
        sec = second;
        min = 0;
        hour = 0;
    }
    if(second >= 60 && second < 3600)
    {
        sec = second%60;
        min = second/60;
        hour = 0;
    }
    if(second >= 3600 )
    {
        sec = second%60;
        min = (second/60)%60;
        hour = second/3600;
    }
    time.setHMS(hour,min,sec);
    return time;

}


void MyPlay::play(QString fileName)
{
    QString args = "mplayer ";

    qDebug() << playStatus;
//    if(playStatus = true)
//    {
//      playProcess->close();
//    }

    playProcess = new QProcess(screamShow);

    connect(playProcess,SIGNAL(readyReadStandardOutput()),screamShow,SLOT(dataRecieve()));
    args += "-slave -quiet ";
    args += fileName;
    args += " -wid " ;
    args += QString::number(screamShow->winId());
    qDebug() << args;
    playProcess->start(args);
    playStatus = true;

}
#if 1
void MyPlay::dataRecieve()
{
    playProcess->write("get_time_length\n");
    playProcess->write("get_time_pos\n");
    playProcess->write("get_percent_pos\n");

    while(playProcess->canReadLine())
    {
        QByteArray retbuf = playProcess->readLine();
        qDebug() << retbuf;
        if(retbuf.startsWith("ANS_TIME_POSITION"))
        {
            retbuf.replace(QByteArray("\n") ,QByteArray(""));
            QString sp(retbuf);
            currentStr = sp.mid(18);

        }
        else if(retbuf.startsWith("ANS_LENGTH"))
        {
            retbuf.replace(QByteArray("\n") ,QByteArray(""));
            QString sp(retbuf);
            totalTime = sp.mid(11);
           // qDebug() << " currentStr" << currentStr;

            int runTime = currentStr.toDouble();
            int allTime = totalTime.toDouble();
            QTime time = int_to_time(runTime);

            qDebug() << runTime;
            qDebug() << allTime;
            slider->setMinimum(0);
            slider->setMaximum(allTime);
            slider->setValue(runTime);

            timelab->setText(time.toString("hh:mm:ss") );
        }

    }

}
#endif

void MyPlay::slotSliderCtl(int value)
{
   playProcess->write(QString("seek "+QString::number(value)+" 2\n").toUtf8());

}


void MyPlay::slotOpen()
{
   QString strFile =  QFileDialog::getOpenFileName(NULL,NULL,_strDir,NULL);
   QFileInfo fileInfo = strFile;
   _strDir = fileInfo.filePath();
   play(strFile);

}

void MyPlay::pushStopButton()
{
    playProcess->write("pause\n");

    if(flag%2 == 0)
    {
        btnplay->setIcon(QIcon("./ico/4.ico"));
        disconnect(playProcess,SIGNAL(readyReadStandardOutput()),this,SLOT(dataRecieve()));
    }
    else
    {
        btnplay->setIcon(QIcon("./ico/6.ico"));
        connect(playProcess,SIGNAL(readyReadStandardOutput()),this,SLOT(dataRecieve()));
        playProcess->write("get_time_length\n");
        playProcess->write("get_time_pos\n");
        playProcess->write("get_percent_pos\n");
    }

    flag++;
    qDebug() << flag;

}

void MyPlay::pushCloseButton()
{
     playProcess->write("quit\n");
     //disconnect(playProcess,SIGNAL(readyReadStandardOutput()),this,SLOT(dataRecieve()));
     screamShow->setStyleSheet("background-image:url(./ico/widget.jpg)");
}


int main(int argc, char **argv)
{
    QApplication app(argc,argv);
    MyPlay w;

    w.show();
    return app.exec();
}
